package logic

import (
	"context"
	"errors"
	"short-vid-joy/models/mysql"

	"short-vid-joy/shortrpc/internal/svc"
	"short-vid-joy/shortrpc/shortrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReplyCommentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReplyCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReplyCommentLogic {
	return &ReplyCommentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReplyCommentLogic) ReplyComment(in *shortrpc.ReplyCommentRequest) (*shortrpc.ReplyCommentResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var video mysql.Video

	if err := video.FindVideoById(mysql.DB, in.VideoId); err != nil {
		return nil, errors.New("视频不存在")
	}

	var findComment mysql.Comment

	if err := findComment.FindCommentById(mysql.DB, in.CommentId); err != nil {
		return nil, errors.New("评论不存在")
	}

	comment := &mysql.Comment{
		UserId:  in.UserId,
		VideoId: in.VideoId,
		Content: in.Content,
		ReplyId: in.ReplyId,
	}

	if err := comment.CreateComment(mysql.DB); err != nil {
		return nil, err
	}

	return &shortrpc.ReplyCommentResponse{
		CommentId: int64(comment.ID),
	}, nil
}
